package com.farm.configer.form.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.servlet.ModelAndView;

import com.farm.configer.domain.Configer;
import com.farm.configer.domain.Configerform;
import com.farm.configer.domain.ex.ConfigerMetaPara;
import com.farm.configer.form.FormModel;
import com.farm.configer.form.FormModelUtils;
import com.farm.configer.service.ConfigerServiceInter;
import com.farm.core.page.ViewMode;
import com.farm.util.spring.BeanFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 自定义模型(通过用户在界面手动录入)
 * 
 * @author Wd
 *
 */
public class NoneConfigerModelImpl extends FormModelUtils implements FormModel {
	private ConfigerServiceInter configerServiceImpl = (ConfigerServiceInter) BeanFactory
			.getBean("configerServiceImpl");
	private Configer configer;

	public NoneConfigerModelImpl(Configer configer) {
		this.configer = configer;
	}

	@Override
	public String getFormPageUrl() {
		return "confweb/baseForm.do?configerid=" + configer.getId();
	}

	@Override
	public String getFormPageSubmitUrl() {
		return "confweb/baseSubmitForm.do?configerid=" + configer.getId();
	}

	@Override
	public ModelAndView getFormPageModelAndView(List<ConfigerMetaPara> testParaList, Object jspConfiger) {
		List<Configerform> forms = configerServiceImpl.getForms(configer.getId(), null);
		for (Configerform form : forms) {
			form.setPcontent(form.getPcontent().replace("\"", ""));
		}
		return ViewMode.getInstance().putAttr("forms", forms).putAttr("attrs", jspConfiger)
				.returnModelAndView("web-configer-forms/commons/baseForm");
	}

	@Override
	public Map<String, Object> submitForm(List<ConfigerMetaPara> paraList) {
		Map<String, Object> configerMap = new HashMap<String, Object>();
		Map<String, Object> paras = new HashMap<String, Object>();
		for (ConfigerMetaPara node : paraList) {
			paras.put(node.getCode(), node.getJsonString());
		}
		configerMap.put(configer.getCode(), paras);
		return configerMap;
	}

	@Override
	public List<Configerform> getForms() {
		// 返回空，則不重置表單
		return null;
	}

}
